package com.chat.service;

import com.chat.bean.Login;
import com.chat.bean.Register;
import com.chat.bean.Userinfo;
import com.chat.mapper.LoginMapper;
import com.chat.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class LoginService {
    @Autowired
    LoginMapper loginMapper;
    public String justLogin(Login login){
        return loginMapper.justLogin(login);
    }
    public String lkUseridByUsername(String username){
        return loginMapper.lkUseridByUsername(username);
    }

    public Userinfo getInfo(String userid) {
        return loginMapper.getInfoById(userid);
    }

    public boolean register(Register register) {
        if (register.getPassword() == null || register.getPassword().equals("") || register.getNickname() == null || register.getNickname().equals("")
            || register.getUsername() == null || register.getUsername().equals("")) {
            return false;
        }
        register.setPassword(Md5Util.StringInMd5(register.getPassword()));
        Login login = new Login();
        String userid = UUID.randomUUID().toString();
        login.setUserid(userid);
        login.setPassword(register.getPassword());
        login.setUsername(register.getUsername());
        boolean a = loginMapper.saveLogin(login);
        if (!a) {
            return false;
        }
        Userinfo userinfo = new Userinfo();
        userinfo.setNickname(register.getNickname());
        userinfo.setUserid(userid);
        userinfo.setUimg(register.getUimg());
        userinfo.setUsign(register.getUsign());
        return loginMapper.saveUser(userinfo);
    }
}
